import Mock from 'mockjs'

const { data } = Mock.mock({
    "data|75": [
        {
            'task_title': "@cword(5, 10)",
            'task_id': "@id()",
            'task_customer': "@cname()",
            'task_stattus': "@boolean()",
            'task_startTime': "@date(2022-MM-dd hh:mm:ss)",
            'task_endTime': "@date(2022-MM-dd hh:mm:ss)",
            'task_goods': "梅赛德斯"
        }
    ]
})

//通过字符串匹配来截取字符串提取传输参数
const getQueryStr = (url, value) => {
    const indexNum = url.indexOf('?')
    if (indexNum !== -1) {
        //截取字符串并匹配
        const queryStrArr = url.substr(indexNum + 1).split('&')
        for (let i = 0; i < queryStrArr.length; i++) {
            //截取字符串并匹配
            const itemArr = queryStrArr[i].split('=')
            //    返回参数
            if (itemArr[0] === value) {
                return itemArr[1]
            }
        }
    }
    return null
}

// 字符串匹配截取请求单个参数的数据
const getQueryStatusStr = (url) => {
    const indexNum = url.indexOf('?')
    if (indexNum !== -1) {
        //截取字符串并匹配
        const queryStrArr = url.substr(indexNum + 1).split('=')
        const StatusStrArr =  queryStrArr[1].split('&')
        return StatusStrArr[0]
    }
    return null
}

// 分页函数 根据参数获取
const pagination = (options, list) => {
    // options 为Obj 类型
    const currentPage = getQueryStr(options.url, "currentPage")
    const pageSize = getQueryStr(options.url, "pageSize")

    // 数据的起始位置:(pageindex-1)*pagesize数据的结束位置: pageindex*pagesize
    //截取数据的起始位置
    const start = (currentPage - 1) * pageSize
    //截取数据的终点位置
    const end = currentPage * pageSize
    //计算总页数
    const totalPage = Math.ceil(list.length / pageSize)
    // 数据截取
    const dataList = currentPage > totalPage ? [] : list.slice(start, end)

    return dataList
}

export default {
    // 所有数组的数据
    getcompleteTaskData: (options) => {
        const dataList = pagination(options, data)
        return {
            "status": 200,
            list: dataList,
            total: data.length
        }
    },

    // 过滤出进行中的数据 根据status进行过滤
    getcompleteTaskStatusData: (options) => {
        console.log(options)
        const statusVal = getQueryStatusStr(options.url)
        console.log(statusVal)
        let dataFilter = []
        if (statusVal === 'true') {
            dataFilter = data.filter(item => item.task_stattus === true)
        } else {
            dataFilter = data.filter(item => item.task_stattus !== true)
        }

        const dataFikterList = pagination(options, dataFilter)

        return {
            "status": 200,
            list: dataFikterList,
            total: dataFilter.length
        }
    }
}
